package com.sun.service.ums.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.mapper.ums.UmsRoleMapper;
import com.sun.pojo.UmsAdmin;
import com.sun.mapper.ums.UmsAdminMapper;
import com.sun.pojo.UmsRole;
import com.sun.service.ums.IUmsAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 妙妙屋
 * @since 2021-12-08
 */
@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements IUmsAdminService {

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Autowired
    private UmsRoleMapper roleMapper;

    @Override
    public UmsAdmin queryUmsAdminInfo(String admin) {
        return umsAdminMapper.queryUmsAdminInfo(admin);
    }

    @Override
    public IPage<UmsAdmin> selectPageAdmin(String keyword, String pageNum, String pageSize) {
        Page<UmsAdmin> p = new Page<>();
        //设置第几页
        p.setCurrent(Long.parseLong(pageNum));
        //设置页大小
        p.setSize(Long.parseLong(pageSize));

        //设置条件
        QueryWrapper<UmsAdmin> qr = new QueryWrapper<>();
        if(keyword != null)
        qr.like("username",keyword)
                .or()
                .like("nick_name",keyword);

        return umsAdminMapper.selectPage(p,qr);
    }

    @Override
    public List<UmsRole> getRoleByAdminId(Long id) {
        return roleMapper.getRoleByAdminId(id);
    }

    @Override
    public Integer deleteAllRoles(Long adminId) {

        return umsAdminMapper.deleteAllRoles(adminId);
    }

    @Override
    public Integer saveBatchRoles(Long adminId, String roleIds) {
        //   adminId   5    roleIds   1,2,3
        //   (5,1)   (5,2)   (5,3)
        String[] split = roleIds.split(",");
        Integer it = 0;
        for (String rid : split) {
            Map map = new HashMap();
            map.put("adminId",adminId);
            map.put("rid",rid);
            Integer integer = umsAdminMapper.saveRoles(map);
            it+=integer;
        }
        return it;
    }
}
